
#######Public Opinion Toward Black Female Political Elites#####
#####by Sydney Carr########
###Study 1 Replication Code########

####first, install relevant packages
install.packages("stargazer")
install.packages("effects")
install.packages("foreign")
install.packages("readtext")
install.packages("readr")
install.packages("clubSandwich")
install.packages("estimatr")
install.packages("modelsummary")

library(stargazer)
library(effects)
library(foreign)
library(readtext)
library(readr)
library(clubSandwich)
library(estimatr)
library(modelsummary)

###set working directory
WD <- "/Users/scarr/Documents/"
##read in the csv file
B <- read.csv(paste0(WD,"study1_JOP.csv"))

##only keep respondents who answered 1 (yes) on Q2 about consent
C <- subset(B, B$Q2 == 1)


###########Recodes###########################


##Political Figures###
###recode perceptions on the strong leader, cares about people like me, and angry/aggressive trait
###directed towards each of the 12 political figures in the study

##Kamala Harris##

C$KH_strong <- C$Q4_1
C$KH_cares <- C$Q4_2
C$KH_ang <- C$Q4_3

##Joe Biden##

C$JB_strong <- C$Q5_1
C$JB_cares <- C$Q5_2
C$JB_ang <- C$Q5_3

##Nancy Pelosi##

C$NP_strong <- C$Q6_1
C$NP_cares <- C$Q6_2
C$NP_ang <- C$Q6_3


##Jim Clyburn##

C$JC_strong <- C$Q7_1
C$JC_cares <- C$Q7_2
C$JC_ang <- C$Q7_3


##Maxine Waters##

C$MW_strong <- C$Q8_1
C$MW_cares <- C$Q8_2
C$MW_ang <- C$Q8_3

##Hakeem Jeffries##

C$HJ_strong <- C$Q9_1
C$HJ_cares <- C$Q9_2
C$HJ_ang <- C$Q9_3

##Carolyn Maloney##

C$CM_strong <- C$Q10_1
C$CM_cares <- C$Q10_2
C$CM_ang <- C$Q10_3

##Jim Himes##

C$JH_strong <- C$Q11_1
C$JH_cares <- C$Q11_2
C$JH_ang <- C$Q11_3

##Marcia Fudge##

C$MF_strong <- C$Q12_1
C$MF_cares <- C$Q12_2
C$MF_ang <- C$Q12_3

##Pete Buttigieg##

C$PB_strong <- C$Q13_1
C$PB_cares <- C$Q13_2
C$PB_ang <- C$Q13_3


##Lloyd Austin##

C$LA_strong <- C$Q14_1
C$LA_cares <- C$Q14_2
C$LA_ang <- C$Q14_3

##Janet Yellen##

C$JY_strong <- C$Q15_1
C$JY_cares <- C$Q15_2
C$JY_ang <- C$Q15_3


##Strong Leadership
C$KH_strong <- (C$KH_strong* (-1) + 5 ) / 4
C$JB_strong <- (C$JB_strong* (-1) + 5 ) / 4
C$NP_strong <- (C$NP_strong* (-1) + 5 ) / 4
C$JC_strong <- (C$JC_strong* (-1) + 5 ) / 4
C$MW_strong <- (C$MW_strong* (-1) + 5 ) / 4
C$HJ_strong <- (C$HJ_strong* (-1) + 5 ) / 4
C$CM_strong <- (C$CM_strong* (-1) + 5 ) / 4
C$JH_strong <- (C$JH_strong* (-1) + 5 ) / 4
C$MF_strong <- (C$MF_strong* (-1) + 5 ) / 4
C$PB_strong <- (C$PB_strong* (-1) + 5 ) / 4
C$LA_strong <- (C$LA_strong* (-1) + 5 ) / 4
C$JY_strong <- (C$JY_strong* (-1) + 5 ) / 4

##Cares##

C$KH_cares <- (C$KH_cares* (-1) + 5 ) / 4
C$JB_cares <- (C$JB_cares* (-1) + 5 ) / 4
C$NP_cares <- (C$NP_cares* (-1) + 5 ) / 4
C$JC_cares <- (C$JC_cares* (-1) + 5 ) / 4
C$MW_cares <- (C$MW_cares* (-1) + 5 ) / 4
C$HJ_cares <- (C$HJ_cares* (-1) + 5 ) / 4
C$CM_cares <- (C$CM_cares* (-1) + 5 ) / 4
C$JH_cares <- (C$JH_cares* (-1) + 5 ) / 4
C$MF_cares <- (C$MF_cares* (-1) + 5 ) / 4
C$PB_cares <- (C$PB_cares* (-1) + 5 ) / 4
C$LA_cares <- (C$LA_cares* (-1) + 5 ) / 4
C$JY_cares <- (C$JY_cares* (-1) + 5 ) / 4


##Angry/ Aggressive##

C$KH_ang <- (C$KH_ang* (-1) + 5 ) / 4
C$JB_ang <- (C$JB_ang* (-1) + 5 ) / 4
C$NP_ang <- (C$NP_ang* (-1) + 5 ) / 4
C$JC_ang <- (C$JC_ang* (-1) + 5 ) / 4
C$MW_ang <- (C$MW_ang* (-1) + 5 ) / 4
C$HJ_ang <- (C$HJ_ang* (-1) + 5 ) / 4
C$CM_ang <- (C$CM_ang* (-1) + 5 ) / 4
C$JH_ang <- (C$JH_ang* (-1) + 5 ) / 4
C$MF_ang <- (C$MF_ang* (-1) + 5 ) / 4
C$PB_ang <- (C$PB_ang* (-1) + 5 ) / 4
C$LA_ang <- (C$LA_ang* (-1) + 5 ) / 4
C$JY_ang <- (C$JY_ang* (-1) + 5 ) / 4


##Controls##

##Gender#
C$gender <- C$Q31

#Race#
C$race <- C$Q32

#Education#
C$edu <- C$Q33

#Age#
C$age <- C$Q34


#Gender#
#recode so that 0= female, 1=male, non-binary and no preference= na
C$gender[C$gender == 1] <- 1
C$gender[C$gender == 2] <- 0
C$gender[C$gender == 3] <- NA
C$gender[C$gender == 4] <- NA
summary(C$gender)

#Race#
#recode so that 0= black, 1= white, everyone else= NA
#create new variable for white, and new variable for black

C$race <- as.character(C$race)


#want to split black and white respondents into separate variables
ls_x <- strsplit(C$race, ",")
white <- rep(0, nrow(C))
black <- rep(0, nrow(C))
for (i in seq(1,nrow(C))){
  white[i] <- ifelse("1" %in% ls_x[[i]], T, F)
  black[i] <- ifelse("2" %in% ls_x[[i]], T, F)
}

summary(white)

table(white)
table(black)

C$race_white <- white
C$race_black <- black

sum(white == T & black == T)

#Education#
#recode so that 0= high school or less, and 1= college or more
C$edu[C$edu==1] <- 0
C$edu[C$edu==2] <- 0
C$edu[C$edu==3] <- 1
C$edu[C$edu==4] <- 1
C$edu[C$edu==5] <- 1
summary(C$edu)


#Party ID
#recode on the 7 point scale,0 = dem, 1=rep
C$pid <- NA
C$pid[C$Q36==1] <- 1
C$pid[C$Q36==2] <- 2
C$pid[C$Q38==2] <- 3
C$pid[C$Q38==3] <- 4
C$pid[C$Q35==4] <- 4
C$pid[C$Q35==5] <- 4
C$pid[C$Q38==1] <- 5
C$pid[C$Q37==2] <- 6
C$pid[C$Q37==1] <- 7
C$pid <- (C$pid*(-1) + 7 ) / 6
summary(C$pid)

#Age
C$age <- as.numeric(C$age)

##Racial Resentment

#special favors , #1=most liberal (strongly disagree), 5= most conservative (strongly agree)

C$Q17[C$Q17 == 1] <- 1
C$Q17[C$Q17 == 2] <- 0.75
C$Q17[C$Q17 == 3] <- 0.5
C$Q17[C$Q17 == 4] <- 0.25
C$Q17[C$Q17 == 5] <- 0


#deserve
#1=most liberal (strongly agree), 5= most conservative (strongly disagree)
C$Q18[C$Q18 == 1] <- 0
C$Q18[C$Q18 == 2] <- 0.25
C$Q18[C$Q18 == 3] <- 0.5
C$Q18[C$Q18 == 4] <- 0.75
C$Q18[C$Q18 == 5] <- 1

##try harder
#1=most liberal (strongly disagree), 5= most conservative (strongly agree)
C$Q19[C$Q19 == 1] <- 1
C$Q19[C$Q19 == 2] <- 0.75
C$Q19[C$Q19 == 3] <- 0.5
C$Q19[C$Q19 == 4] <- 0.25
C$Q19[C$Q19 == 5] <- 0


##slavery/discrimination
#1= most liberal (strongly agree), 5= most conservative (strongly disagree)

C$Q20[C$Q20 == 1] <- 0
C$Q20[C$Q20 == 2] <- 0.25
C$Q20[C$Q20 == 3] <- 0.75
C$Q20[C$Q20 == 4] <- 0.5
C$Q20[C$Q20 == 5] <- 1


# Create index for racial resentment
C$special_favor <- as.numeric(C$Q17)
C$deserve <- as.numeric(C$Q18)
C$try_harder <- as.numeric(C$Q19)
C$discrimrace <- as.numeric(C$Q20)
C$racial_index <- C$special_favor + C$deserve + C$try_harder + C$discrimrace

summary(C$racial_index)

##Modern Sexism
#want the 1s to be most sexist and 0s to be most liberal
#cross tab ##if want to see a cross tab, example would be: table(C$Q21, C$Q22)


#1= strongly agree (most sexist), 0=strongly disagree (most liberal)

#discrim gen
C$Q21[C$Q21 == 1] <- 1
C$Q21[C$Q21 == 2] <- 0.75
C$Q21[C$Q21 == 3] <- 0.5
C$Q21[C$Q21 == 4] <- 0.25
C$Q21[C$Q21 == 5] <- 0
summary(C$Q21)
table(C$Q21)

#good jobs
#1=strongly disagree (most sexist), 0-strongly agree (most liberal)
C$Q22[C$Q22 == 1] <- 0
C$Q22[C$Q22 == 2] <- 0.25
C$Q22[C$Q22 == 3] <- 0.5
C$Q22[C$Q22 == 4] <- 0.75
C$Q22[C$Q22 == 5] <- 1
summary(C$Q22)
table(C$Q22)

#women tv
#1=strongly agree (most sexist), 0=strongly disagree (most liberal)

C$Q23[C$Q23 == 1] <- 1
C$Q23[C$Q23 == 2] <- 0.75
C$Q23[C$Q23 == 3] <- 0.5
C$Q23[C$Q23 == 4] <- 0.25
C$Q23[C$Q23 == 5] <- 0
summary(C$Q23)
table(C$Q23)

#equal wives
#1=strongly agree (most sexist), 0=strongly disagree (most liberal)

C$Q24[C$Q24 == 1] <- 1
C$Q24[C$Q24 == 2] <- 0.75
C$Q24[C$Q24 == 3] <- 0.5
C$Q24[C$Q24 == 4] <- 0.25
C$Q24[C$Q24 == 5] <- 0
summary(C$Q24)
table(C$Q24)

#equal opp
#1=strongly agree (most sexist), 0=strongly disagree (most liberal)

C$Q25[C$Q25 == 1] <- 1
C$Q25[C$Q25 == 2] <- 0.75
C$Q25[C$Q25 == 3] <- 0.5
C$Q25[C$Q25 == 4] <- 0.25
C$Q25[C$Q25 == 5] <- 0
summary(C$Q25)
table(C$Q25)

#anger
#1=strongly disagree (most sexist), 0-strongly agree (most liberal)
C$Q26[C$Q26 == 1] <- 0
C$Q26[C$Q26 == 2] <- 0.25
C$Q26[C$Q26 == 3] <- 0.5
C$Q26[C$Q26 == 4] <- 0.75
C$Q26[C$Q26 == 5] <- 1
summary(C$Q26)
table(C$Q26)

#limits
#1=strongly disagree (most sexist), 0-strongly agree (most liberal)

C$Q27[C$Q27 == 1] <- 0
C$Q27[C$Q27 == 2] <- 0.25
C$Q27[C$Q27 == 3] <- 0.5
C$Q27[C$Q27 == 4] <- 0.75
C$Q27[C$Q27 == 5] <- 1
summary(C$Q27)
table(C$Q27)
#gov
#1=strongly agree (most sexist), 0=strongly disagree (most liberal)

C$Q28[C$Q28 == 1] <- 1
C$Q28[C$Q28 == 2] <- 0.75
C$Q28[C$Q28 == 3] <- 0.5
C$Q28[C$Q28 == 4] <- 0.25
C$Q28[C$Q28 == 5] <- 0
summary(C$Q28)
table(C$Q28)


# Create index for modern sexism
C$discrimgen <- as.numeric(C$Q21)
C$good_jobs <- as.numeric(C$Q22)
C$wom_tv <- as.numeric(C$Q23)
C$equal_wives <- as.numeric(C$Q24)
C$equal_opp <- as.numeric(C$Q25)
C$anger <- as.numeric(C$Q26)
C$limits <- as.numeric(C$Q27)
C$gov <- as.numeric(C$Q28)

C$sexism_index <- C$discrimgen + C$good_jobs + C$wom_tv + C$equal_wives + C$equal_opp
+ C$anger + C$limits + C$gov

################Grouping High/Low Racial Resent/Mod Sexism: 4 groups

C$sexrace <- NA
#low on sexism and low on racism
C$sexrace[C$sexism_index<2.5 & C$racial_index<2.1] <- "1 low/low"
#high on sexism and low on racism
C$sexrace[C$sexism_index>2.49 & C$racial_index<2.1] <- "2 high/low"
#low on sexism and high on racism
C$sexrace[C$sexism_index<2.5 & C$racial_index>2] <- "3 low/high"
#high on sexism and high on racism
C$sexrace[C$sexism_index>2.49 & C$racial_index>2] <- "4 high/high"

###number of respondents in each category
###according to mod sexism and racial resentment
table(C$sexrace)


##Create Index for each characteristic (strong leader, cares, and angry) into the 4 race-gender groups

#Black Women

C$black_women_strong_index <- C$KH_strong + C$MW_strong + C$MF_strong
C$black_women_cares_index <- C$KH_cares + C$MW_cares + C$MF_cares
C$black_women_ang_index <- C$KH_ang + C$MW_ang + C$MF_ang

summary(C$black_women_strong_index)
table(C$black_women_strong_index)
##convert 0 to 1
C$black_women_strong_index <- (C$black_women_strong_index - min(0)) / (max(C$black_women_strong_index) - min(C$black_women_strong_index))

C$black_women_cares_index <- (C$black_women_cares_index - min(0)) / (max(C$black_women_cares_index) - min(C$black_women_cares_index))

C$black_women_ang_index <- (C$black_women_ang_index - min(0)) / (max(C$black_women_ang_index) - min(C$black_women_ang_index))


#Black Men


C$black_men_strong_index <- C$JC_strong + C$HJ_strong + C$LA_strong
C$black_men_cares_index <-  C$JC_cares + C$HJ_cares + C$LA_cares
C$black_men_ang_index <-  C$JC_ang + C$HJ_ang + C$LA_ang

##convert 0 to 1

C$black_men_strong_index <- (C$black_men_strong_index - min(0)) / (max(C$black_men_strong_index) - min(C$black_men_strong_index))

C$black_men_cares_index <- (C$black_men_cares_index - min(0)) / (max(C$black_men_cares_index) - min(C$black_men_cares_index))

C$black_men_ang_index <- (C$black_men_ang_index - min(0)) / (max(C$black_men_ang_index) - min(C$black_men_ang_index))


##White Women

C$wht_women_strong_index <- C$NP_strong + C$CM_strong + C$JY_strong
C$wht_women_cares_index <- C$NP_cares + C$CM_cares + C$JY_cares
C$wht_women_ang_index <- C$NP_ang + C$CM_ang + C$JY_ang


##convert 0 to 1

C$wht_women_strong_index <- (C$wht_women_strong_index - min(0)) / (max(C$wht_women_strong_index) - min(C$wht_women_strong_index))
C$wht_women_cares_index <- (C$wht_women_cares_index - min(0)) / (max(C$wht_women_cares_index) - min(C$wht_women_cares_index))
C$wht_women_ang_index <- (C$wht_women_ang_index - min(0)) / (max(C$wht_women_ang_index) - min(C$wht_women_ang_index))

##White Men

C$wht_men_strong_index <- C$JB_strong + C$JH_strong + C$PB_strong
C$wht_men_cares_index <- C$JB_cares + C$JH_cares + C$PB_cares
C$wht_men_ang_index <- C$JB_ang + C$JH_ang + C$PB_ang

##convert 0 to 1

C$wht_men_strong_index <- (C$wht_men_strong_index - min(0)) / (max(C$wht_men_strong_index) - min(C$wht_men_strong_index))
C$wht_men_cares_index <- (C$wht_men_cares_index - min(0)) / (max(C$wht_men_cares_index) - min(C$wht_men_cares_index))
C$wht_men_ang_index <- (C$wht_men_ang_index - min(0)) / (max(C$wht_men_ang_index) - min(C$wht_men_ang_index))




            ####################Panel Estimation###################



##first start with creating a new dataframe
##stack respondents 4 responses per respondent according to race-gender group

## strong leader variable
ivs <- c('ResponseId', 'sexrace', 'pid', 'race_white', 'gender')
dvs <- c('black_women_strong_index', 'black_men_strong_index', 'wht_women_strong_index', 'wht_men_strong_index')

a <- C[,c(ivs,dvs[1])]
b <- C[,c(ivs,dvs[2])]
c <- C[,c(ivs,dvs[3])]
d <- C[,c(ivs,dvs[4])]


names(a)[names(a) == 'black_women_strong_index'] <- 'strong_index'
names(b)[names(b) == 'black_men_strong_index'] <- 'strong_index'
names(c)[names(c) == 'wht_women_strong_index'] <- 'strong_index'
names(d)[names(d) == 'wht_men_strong_index'] <- 'strong_index'

a$treatment <- "1 black women"
b$treatment <- "2 black men"
c$treatment <- "3 white women"
d$treatment <- "4 white men"

D <- rbind(a,b,c,d)


## cares variable

ivs <- c('ResponseId', 'sexrace', 'pid', 'race_white', 'gender')
dvs <- c('black_women_cares_index', 'black_men_cares_index', 'wht_women_cares_index', 'wht_men_cares_index')

e <- C[,c(ivs,dvs[1])]
f <- C[,c(ivs,dvs[2])]
g <- C[,c(ivs,dvs[3])]
h <- C[,c(ivs,dvs[4])]


names(e)[names(e) == 'black_women_cares_index'] <- 'cares_index'
names(f)[names(f) == 'black_men_cares_index'] <- 'cares_index'
names(g)[names(g) == 'wht_women_cares_index'] <- 'cares_index'
names(h)[names(h) == 'wht_men_cares_index'] <- 'cares_index'

e$treatment <- "1 black women"
f$treatment <- "2 black men"
g$treatment <- "3 white women"
h$treatment <- "4 white men"

F <- rbind(e,f,g,h)

View(F)


###angry/aggressive###

ivs <- c('ResponseId', 'sexrace', 'pid', 'race_white', 'gender')
dvs <- c('black_women_ang_index', 'black_men_ang_index', 'wht_women_ang_index', 'wht_men_ang_index')

i <- C[,c(ivs,dvs[1])]
j <- C[,c(ivs,dvs[2])]
k <- C[,c(ivs,dvs[3])]
l <- C[,c(ivs,dvs[4])]


names(i)[names(i) == 'black_women_ang_index'] <- 'ang_index'
names(j)[names(j) == 'black_men_ang_index'] <- 'ang_index'
names(k)[names(k) == 'wht_women_ang_index'] <- 'ang_index'
names(l)[names(l) == 'wht_men_ang_index'] <- 'ang_index'

i$treatment <- "1 black women"
j$treatment <- "2 black men"
k$treatment <- "3 white women"
l$treatment <- "4 white men"

G <- rbind(i,j,k,l)


###############clustered standard errors testing#######


##strong leader

mod.clust_strong <- lm_robust(strong_index ~ sexrace*treatment + 
                                  pid*treatment + 
                                  race_white + 
                                  gender, 
                                clusters = ResponseId,
                                data = D)
summary(mod.clust_strong)

mod_strong <- lm(strong_index~  sexrace*treatment + pid*treatment + race_white + gender, 
               data = D)

summary(mod_strong)

##cares

mod.clust_cares <- lm_robust(cares_index ~ sexrace*treatment + 
                                pid*treatment + 
                                race_white + 
                                gender, 
                              clusters = ResponseId,
                              data = F)
summary(mod.clust_cares)

mod_cares <- lm(cares_index~  sexrace*treatment + pid*treatment + race_white + gender, 
                 data = F)

summary(mod_cares)
##angry

mod.clust_ang <- lm_robust(ang_index ~ sexrace*treatment + 
                                pid*treatment + 
                                race_white + 
                                gender, 
                              clusters = ResponseId,
                              data = G)
summary(mod.clust_ang)

mod_ang <- lm(ang_index~  sexrace*treatment + pid*treatment + race_white + gender, 
              data = G)


summary(mod_ang)

#####################Create Tables for Study 1 in Appendix: Tables 1.1, 1.2, and 1.3########
#############create the tables to output to an html using stargazer#############
#########for the purposes of the paper, the tables here were then converted into word doc tables#######
####these html files will output onto laptop/computer####
###Table 1.1 (Appendix)
##Strong Leader Characteristic
stargazer(
  mod_strong,
  type = "html",
  align = TRUE,
  dep.var.labels=c("Strong Leadership"),
  covariate.labels = c(
    "high on mod sexism * low on racial resent", "low on mod sexism * high on racial resent",
    "high on mod sexism * high on racial resent", "Black Male Political Figures", 
    "White Female Political Figures", "White Male Political Figures", "Party ID(Democrat)", "Race(White)",
    "Gender(Men)", 
    "high on mod sexism/low on racial resent * Black Male Political Figures", 
    "low on mod sexism/high on racial resent * Black Male Political Figures", 
    "high on mod sexism/high on racial resent * Black Male Political Figures", 
    "high on mod sexism/low on racial resent * White Female Political Figures", 
    "low on mod sexism/high on racial resent * White Female Political Figures", 
    "high on mod sexism/high on racial resent * White Female Political Figures", 
    "high on mod sexism/low on racial resent * White Male Political Figures", 
    "low on mod sexism/high on racial resent * White Male Political Figures", 
    "high on mod sexism/high on racial resent * White Male Political Figures", 
        "Black Male Political Figures * Party ID (Democrat)",
    "White Female Political Figures * Party ID (Democrat)",
    "White Male Political Figures * Party ID(Democrat)"),
     omit.stat=c("LL","ser","f"), no.space=TRUE, 
  keep.stat = c("n", "rsq", "adj.rsq"),
  title = "Study 1: Perceptions on 'Strong Leadership' Trait", out = "study1_strongleader.html")

##Table 1.2 (Appendix)
##Cares About People Like Me Characteristic
stargazer(
  mod_cares,
  type = "html",
  align = TRUE,
  dep.var.labels=c("Cares About People Like Me"),
  covariate.labels = c(
    "high on mod sexism * low on racial resent", "low on mod sexism * high on racial resent",
    "high on mod sexism * high on racial resent", "Black Male Political Figures", 
    "White Female Political Figures", "White Male Political Figures", "Party ID(Democrat)", "Race(White)",
    "Gender(Men)", 
    "high on mod sexism/low on racial resent * Black Male Political Figures", 
    "low on mod sexism/high on racial resent * Black Male Political Figures", 
    "high on mod sexism/high on racial resent * Black Male Political Figures", 
    "high on mod sexism/low on racial resent * White Female Political Figures", 
    "low on mod sexism/high on racial resent * White Female Political Figures", 
    "high on mod sexism/high on racial resent * White Female Political Figures", 
    "high on mod sexism/low on racial resent * White Male Political Figures", 
    "low on mod sexism/high on racial resent * White Male Political Figures", 
    "high on mod sexism/high on racial resent * White Male Political Figures", 
    "Black Male Political Figures * Party ID (Democrat)",
    "White Female Political Figures * Party ID (Democrat)",
    "White Male Political Figures * Party ID(Democrat)"),
  omit.stat=c("LL","ser","f"), no.space=TRUE, 
  keep.stat = c("n", "rsq", "adj.rsq"),
  title = "Study 1: Perceptions on 'Cares' Trait", out = "study1_cares.html")

##Table 1.3 (Appendix)
##Angry/Aggressive Characteristic
stargazer(
  mod_ang,
  type = "html",
  align = TRUE,
  dep.var.labels=c("Angry/Aggressive"),
  covariate.labels = c(
    "high on mod sexism * low on racial resent", "low on mod sexism * high on racial resent",
    "high on mod sexism * high on racial resent", "Black Male Political Figures", 
    "White Female Political Figures", "White Male Political Figures", "Party ID(Democrat)", "Race(White)",
    "Gender(Men)", 
    "high on mod sexism/low on racial resent * Black Male Political Figures", 
    "low on mod sexism/high on racial resent * Black Male Political Figures", 
    "high on mod sexism/high on racial resent * Black Male Political Figures", 
    "high on mod sexism/low on racial resent * White Female Political Figures", 
    "low on mod sexism/high on racial resent * White Female Political Figures", 
    "high on mod sexism/high on racial resent * White Female Political Figures", 
    "high on mod sexism/low on racial resent * White Male Political Figures", 
    "low on mod sexism/high on racial resent * White Male Political Figures", 
    "high on mod sexism/high on racial resent * White Male Political Figures", 
    "Black Male Political Figures * Party ID (Democrat)",
    "White Female Political Figures * Party ID (Democrat)",
    "White Male Political Figures * Party ID(Democrat)"),
  omit.stat=c("LL","ser","f"), no.space=TRUE, 
  keep.stat = c("n", "rsq", "adj.rsq"),
  title = "Study 1: Perceptions on 'Angry/Aggressive' Trait", out = "study1_ang.html")


#########Create Figures Located in the Paper: Figures 1.1, 1.2, and 1.3#########
################Point Estimation Figures####################
######these pdf files will output onto laptop/computer######

###Figure 1.1
#####strong leader variable
mod_strong_bw <- lm(black_women_strong_index ~ sexrace + pid, data = C)
mod_strong_bm <- lm(black_men_strong_index ~ sexrace + pid, data = C)
mod_strong_ww <- lm(wht_women_strong_index ~ sexrace + pid, data = C)
mod_strong_wm <- lm(wht_men_strong_index ~ sexrace + pid, data = C)
modelsummary(list(mod_strong_bw,mod_strong_bm, mod_strong_ww, mod_strong_wm),
             stars=T, 
             gof_omit="IC|RMSE|F|Adj")

eff <- effect("sexrace", mod_strong_bw)
effr1 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_strong_bm)
effr2 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_strong_ww)
effr3 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_strong_wm)
effr4 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effr1$treatment <- "1 black women"
effr2$treatment <- "2 black men"
effr3$treatment <- "3 white women"
effr4$treatment <- "4 white men"
effr <- rbind(effr1,effr2,effr3,effr4)
effr$order <- as.numeric(substring(effr$sexrace,1,1))
effr$order2 <- NA
effr$order2[effr$treatment=="1 black women"] <- effr$order[effr$treatment=="1 black women"] - .2
effr$order2[effr$treatment=="2 black men"] <- effr$order[effr$treatment=="2 black men"] - .1
effr$order2[effr$treatment=="3 white women"] <- effr$order[effr$treatment=="3 white women"] 
effr$order2[effr$treatment=="4 white men"] <- effr$order[effr$treatment=="4 white men"] + .1

{
  pdf("study1_strongleader_figure.pdf", width = 6, height = 5)
  plot(effr$order, effr$fit,  type="n", ann=FALSE, axes=FALSE, ylim=c(.3,.6), xlim=c(.5,4.5))
  p <- effr[effr$treatment=="1 black women",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=15)
  p <- effr[effr$treatment=="2 black men",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=16)
  p <- effr[effr$treatment=="3 white women",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=17)
  p <- effr[effr$treatment=="4 white men",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=18)
  axis(1, at=c(1,2,3,4), labels=c("Low Sexism\nLow Racism", 
                                  "High Sexism\nLow Racism", 
                                  "Low Sexism\nHigh Racism",
                                  "High Sexism\nHigh Racism"), col="white", cex.axis=.8)
  axis(2, las=1, at=c(.3,.4,.5,.6)) 
  legend(3, .6, legend=c("Black Women", 
                         "Black Men", 
                         "White Women",
                         "White Men"),
         col=c("black", "black", "black", "black"), pch=c(15,16,17,18), cex=0.8, box.lty=0)
  mtext(side=1, "Respondent Sexism/Racism Categories", line=3)
  mtext(side=2, "Strong Leader", line=3)
  dev.off()
}

####Figure 1.2
#####cares variable
mod_cares_bw <- lm(black_women_cares_index ~ sexrace + pid, data = C)
mod_cares_bm <- lm(black_men_cares_index ~ sexrace + pid, data = C)
mod_cares_ww <- lm(wht_women_cares_index ~ sexrace + pid, data = C)
mod_cares_wm <- lm(wht_men_cares_index ~ sexrace + pid, data = C)
modelsummary(list(mod_cares_bw,mod_cares_bm, mod_cares_ww, mod_cares_wm),
             stars=T, 
             gof_omit="IC|RMSE|F|Adj")

eff <- effect("sexrace", mod_cares_bw)
effr1 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_cares_bm)
effr2 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_cares_ww)
effr3 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_cares_wm)
effr4 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effr1$treatment <- "1 black women"
effr2$treatment <- "2 black men"
effr3$treatment <- "3 white women"
effr4$treatment <- "4 white men"
effr <- rbind(effr1,effr2,effr3,effr4)
effr$order <- as.numeric(substring(effr$sexrace,1,1))
effr$order2 <- NA
effr$order2[effr$treatment=="1 black women"] <- effr$order[effr$treatment=="1 black women"] - .2
effr$order2[effr$treatment=="2 black men"] <- effr$order[effr$treatment=="2 black men"] - .1
effr$order2[effr$treatment=="3 white women"] <- effr$order[effr$treatment=="3 white women"] 
effr$order2[effr$treatment=="4 white men"] <- effr$order[effr$treatment=="4 white men"] + .1

{
  pdf("study1_cares_figure.pdf", width = 6, height = 5)
  plot(effr$order, effr$fit,  type="n", ann=FALSE, axes=FALSE, ylim=c(.3,.6), xlim=c(.5,4.5))
  p <- effr[effr$treatment=="1 black women",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=15)
  p <- effr[effr$treatment=="2 black men",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=16)
  p <- effr[effr$treatment=="3 white women",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=17)
  p <- effr[effr$treatment=="4 white men",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=18)
  axis(1, at=c(1,2,3,4), labels=c("Low Sexism\nLow Racism", 
                                  "High Sexism\nLow Racism", 
                                  "Low Sexism\nHigh Racism",
                                  "High Sexism\nHigh Racism"), col="white", cex.axis=.8)
  axis(2, las=1, at=c(.3,.4,.5,.6)) 
  legend(3, .6, legend=c("Black Women", 
                         "Black Men", 
                         "White Women",
                         "White Men"),
         col=c("black", "black", "black", "black"), pch=c(15,16,17,18), cex=0.8, box.lty=0)
  mtext(side=1, "Respondent Sexism/Racism Categories", line=3)
  mtext(side=2, "Cares About People Like Me", line=3)
  dev.off()
}

###Figure 1.3
#####angry variable
mod_ang_bw <- lm(black_women_ang_index ~ sexrace + pid, data = C)
mod_ang_bm <- lm(black_men_ang_index ~ sexrace + pid, data = C)
mod_ang_ww <- lm(wht_women_ang_index ~ sexrace + pid, data = C)
mod_ang_wm <- lm(wht_men_ang_index ~ sexrace + pid, data = C)
modelsummary(list(mod_ang_bw,mod_ang_bm, mod_ang_ww, mod_ang_wm),
             stars=T, 
             gof_omit="IC|RMSE|F|Adj")

eff <- effect("sexrace", mod_ang_bw)
effr1 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_ang_bm)
effr2 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_ang_ww)
effr3 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
eff <- effect("sexrace", mod_ang_wm)
effr4 <- cbind(eff$x,fit=eff$fit,lower=eff$lower,higher=eff$upper)
effr1$treatment <- "1 black women"
effr2$treatment <- "2 black men"
effr3$treatment <- "3 white women"
effr4$treatment <- "4 white men"
effr <- rbind(effr1,effr2,effr3,effr4)
effr$order <- as.numeric(substring(effr$sexrace,1,1))
effr$order2 <- NA
effr$order2[effr$treatment=="1 black women"] <- effr$order[effr$treatment=="1 black women"] - .2
effr$order2[effr$treatment=="2 black men"] <- effr$order[effr$treatment=="2 black men"] - .1
effr$order2[effr$treatment=="3 white women"] <- effr$order[effr$treatment=="3 white women"] 
effr$order2[effr$treatment=="4 white men"] <- effr$order[effr$treatment=="4 white men"] + .1

{
  pdf("study1_ang_figure.pdf", width = 6, height = 6)
  plot(effr$order, effr$fit,  type="n", ann=FALSE, axes=FALSE, ylim=c(0,.8), xlim=c(.5,4.5))
  p <- effr[effr$treatment=="1 black women",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=15)
  p <- effr[effr$treatment=="2 black men",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=16)
  p <- effr[effr$treatment=="3 white women",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=17)
  p <- effr[effr$treatment=="4 white men",]
  arrows(p$order2, p$lower, p$order2, p$higher, length=.05, code=3, angle=90, col="gray20",lwd=1,lty=3)
  points(p$order2, p$fit,  col="black", pch=18)
  axis(1, at=c(1,2,3,4), labels=c("Low Sexism\nLow Racism", 
                                  "High Sexism\nLow Racism", 
                                  "Low Sexism\nHigh Racism",
                                  "High Sexism\nHigh Racism"), col="white", cex.axis=.8)
  axis(2, las=1)
  legend(3, .8, legend=c("Black Women", 
                         "Black Men", 
                         "White Women",
                         "White Men"),
         col=c("black", "black", "black", "black"), pch=c(15,16,17,18), cex=0.8, box.lty=0)
  mtext(side=1, "Respondent Sexism/Racism Categories", line=3)
  mtext(side=2, "Angry/Aggressive", line=3)
  dev.off()
}








